package org.automated.realestatemanager.advertisementgetter;

import java.util.LinkedList;
import java.util.List;

import org.apache.log4j.Logger;
import org.automated.realestatemanager.parsers.AdvertisementParser;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.CssSelectorNodeFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

public class PremierNewspaperArchive extends AbstractAdvertisementGetter {

	private static final Logger log = Logger
			.getLogger(PremierNewspaperArchive.class);

	public void executeParcing(AdvertisementParser advParser) {
		advParser.setSource("PremierNewsPaper");

		final List<String> result = new LinkedList<String>();
		final int pages = 8;

		try {
			Parser parser = null;
			for (int k = 0; k < pages; k++) {
				parser = new Parser(
						"http://premier.in.ua/oldarchive.aspx?r=&issue=&rub=83&page="
								+ k);

				parser.setEncoding("utf-8");

				final NodeFilter atrb1 = new CssSelectorNodeFilter(
						".premold_col-1");
				final NodeList nodeList1 = parser.parse(atrb1);

				final NodeFilter atrb2 = new CssSelectorNodeFilter(
						".premold_col-1");
				final NodeList nodeList2 = parser.parse(atrb2);

				for (int i = 0; i < nodeList1.size(); i++) {
					final Node node = nodeList1.elementAt(i);
					String[] texts = node.toHtml()
							.replaceAll("</?div[^>]*>", " ")
							.split("(\\s*)?</?p[^>]*>(\\s*)?");
					for (String s : texts) {
						advParser.executeParcing(s);
						System.out.println("\nCurrent: " + s);
					}
				}

				for (int i = 0; i < nodeList2.size(); i++) {
					final Node node = nodeList2.elementAt(i);
					String[] texts = node.getText()
							.replaceAll("</?div[^>]*>", " ")
							.split("(\\s*)?</?p[^>]*>(\\s*)?");
					for (String s : texts) {
						advParser.executeParcing(s);
						System.out.println("\nCurrent: " + s);
					}
				}
				try {
					Thread.sleep(1000l);
				} catch (final InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		} catch (final ParserException e) {
			e.printStackTrace();
		}

	}

}